{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de61e523",
   "metadata": {},
   "source": [
    "## 1. APP U本位合约行情-实时行情函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3463587",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|get_bookTicker|获取一个产品的最优挂单|\n",
    "|get_bookTickers|获取全部产品的最优挂单（列表格式）|\n",
    "|get_bookTickersMap|获取全部产品的最优挂单 （字典格式）|\n",
    "|get_tickerPrice|获取一个产品的最新价格|\n",
    "|get_tickerPrices|获取全部产品的最新价格（列表格式）|\n",
    "|get_tickerPricesMap|获取全部产品的最新价格 (字典格式)|\n",
    "|get_depth|获取深度信息|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bcedeeb",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef5b96a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceUM\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2b5c55b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceUM = BinanceUM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "market = binanceUM.market"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6045b4cc",
   "metadata": {},
   "source": [
    "## 3. 获取一个产品的最优挂单 get_bookTicker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82df1171",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'BTCUSDT',\n",
      "          'bidPrice': '39980.80',\n",
      "          'bidQty': '0.223',\n",
      "          'askPrice': '39980.90',\n",
      "          'askQty': '9.638',\n",
      "          'time': 1706192853095,\n",
      "          'lastUpdateId': 3886059539004},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "bookTicker_result = market.get_bookTicker(symbol='BTCUSDT')\n",
    "eprint(bookTicker_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "659bd2b0",
   "metadata": {},
   "source": [
    "## 4. 获取全部产品的最优挂单（列表格式）get_bookTickers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37fe3d62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSDT',\n",
      "           'bidPrice': '39980.80',\n",
      "           'bidQty': '0.293',\n",
      "           'askPrice': '39980.90',\n",
      "           'askQty': '7.874',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSDT',\n",
      "           'bidPrice': '2207.40',\n",
      "           'bidQty': '8.099',\n",
      "           'askPrice': '2207.41',\n",
      "           'askQty': '47.500',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'BCHUSDT',\n",
      "           'bidPrice': '237.14',\n",
      "           'bidQty': '4.087',\n",
      "           'askPrice': '237.15',\n",
      "           'askQty': '1.500',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'XRPUSDT',\n",
      "           'bidPrice': '0.5091',\n",
      "           'bidQty': '27610.6',\n",
      "           'askPrice': '0.5092',\n",
      "           'askQty': '50550.5',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'EOSUSDT',\n",
      "           'bidPrice': '0.679',\n",
      "           'bidQty': '40474.3',\n",
      "           'askPrice': '0.680',\n",
      "           'askQty': '282807.7',\n",
      "           '...': '......'},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "bookTickers_result = market.get_bookTickers()\n",
    "eprint(bookTickers_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2c38c85b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSDT',\n",
      "           'bidPrice': '39980.80',\n",
      "           'bidQty': '4.276',\n",
      "           'askPrice': '39980.90',\n",
      "           'askQty': '1.229',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSDT',\n",
      "           'bidPrice': '2207.81',\n",
      "           'bidQty': '36.656',\n",
      "           'askPrice': '2207.82',\n",
      "           'askQty': '2.740',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "bookTickers_result = market.get_bookTickers(symbols=['BTCUSDT', 'ETHUSDT'])\n",
    "eprint(bookTickers_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65007747",
   "metadata": {},
   "source": [
    "## 5. 获取全部产品的最优挂单 （字典格式）get_bookTickersMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1982a949",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'BTCUSDT': {'symbol': 'BTCUSDT',\n",
      "                      'bidPrice': '39980.90',\n",
      "                      'bidQty': '11.650',\n",
      "                      'askPrice': '39981.00',\n",
      "                      'askQty': '0.901',\n",
      "                      '...': '......'},\n",
      "          'ETHUSDT': {'symbol': 'ETHUSDT',\n",
      "                      'bidPrice': '2207.99',\n",
      "                      'bidQty': '75.316',\n",
      "                      'askPrice': '2208.00',\n",
      "                      'askQty': '12.926',\n",
      "                      '...': '......'},\n",
      "          'BCHUSDT': {'symbol': 'BCHUSDT',\n",
      "                      'bidPrice': '237.18',\n",
      "                      'bidQty': '8.734',\n",
      "                      'askPrice': '237.19',\n",
      "                      'askQty': '0.759',\n",
      "                      '...': '......'},\n",
      "          'XRPUSDT': {'symbol': 'XRPUSDT',\n",
      "                      'bidPrice': '0.5091',\n",
      "                      'bidQty': '55089.9',\n",
      "                      'askPrice': '0.5092',\n",
      "                      'askQty': '46667.9',\n",
      "                      '...': '......'},\n",
      "          'EOSUSDT': {'symbol': 'EOSUSDT',\n",
      "                      'bidPrice': '0.679',\n",
      "                      'bidQty': '56550.5',\n",
      "                      'askPrice': '0.680',\n",
      "                      'askQty': '272375.7',\n",
      "                      '...': '......'},\n",
      "          'LTCUSDT': {'symbol': 'LTCUSDT',\n",
      "                      'bidPrice': '65.24',\n",
      "                      'bidQty': '168.506',\n",
      "                      'askPrice': '65.25',\n",
      "                      'askQty': '48.750',\n",
      "                      '...': '......'},\n",
      "          '...': '......'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "bookTickersMap_result = market.get_bookTickersMap()\n",
    "eprint(bookTickersMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0d728ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'BTCUSDT': {'symbol': 'BTCUSDT',\n",
      "                      'bidPrice': '39984.60',\n",
      "                      'bidQty': '5.123',\n",
      "                      'askPrice': '39984.70',\n",
      "                      'askQty': '0.982',\n",
      "                      '...': '......'},\n",
      "          'ETHUSDT': {'symbol': 'ETHUSDT',\n",
      "                      'bidPrice': '2208.35',\n",
      "                      'bidQty': '13.595',\n",
      "                      'askPrice': '2208.36',\n",
      "                      'askQty': '27.052',\n",
      "                      '...': '......'}},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "bookTickersMap_result = market.get_bookTickersMap(symbols=['BTCUSDT', 'ETHUSDT'])\n",
    "eprint(bookTickersMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cdb12b3",
   "metadata": {},
   "source": [
    "## 6. 获取一个产品的最新价格 get_tickerPrice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2ffa0d69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': {'symbol': 'BTCUSDT', 'price': '39984.60', 'time': 1706192863131}, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "tickerPrice_result = market.get_tickerPrice(symbol='BTCUSDT')\n",
    "eprint(tickerPrice_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daac1f01",
   "metadata": {},
   "source": [
    "## 7. 获取全部产品的最新价格（列表格式） get_tickerPrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d039bf69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'CFXUSDT', 'price': '0.1774000', 'time': 1706192858797},\n",
      "          {'symbol': 'BALUSDT', 'price': '3.542', 'time': 1706192858970},\n",
      "          {'symbol': 'STEEMUSDT', 'price': '0.215700', 'time': 1706192863909},\n",
      "          {'symbol': 'RNDRUSDT', 'price': '3.733300', 'time': 1706192864007},\n",
      "          {'symbol': 'CYBERUSDT', 'price': '6.814000', 'time': 1706192863855},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "tickerPrices_result = market.get_tickerPrices()\n",
    "eprint(tickerPrices_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9580ebf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'ETHUSDT', 'price': '2208.68', 'time': 1706192865251},\n",
      "          {'symbol': 'BTCUSDT', 'price': '39990.80', 'time': 1706192865193}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "tickerPrices_result = market.get_tickerPrices(symbols=['BTCUSDT', 'ETHUSDT'])\n",
    "eprint(tickerPrices_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c799fd",
   "metadata": {},
   "source": [
    "## 8. 获取全部产品的最新价格 (字典格式) get_tickerPricesMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3eff5ed8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'TLMUSDT': {'symbol': 'TLMUSDT', 'price': '0.0133500', 'time': 1706192856376},\n",
      "          'ICXUSDT': {'symbol': 'ICXUSDT', 'price': '0.2186', 'time': 1706192864162},\n",
      "          'TRUUSDT': {'symbol': 'TRUUSDT', 'price': '0.0442200', 'time': 1706192850693},\n",
      "          'KASUSDT': {'symbol': 'KASUSDT', 'price': '0.0976400', 'time': 1706192865635},\n",
      "          'STMXUSDT': {'symbol': 'STMXUSDT', 'price': '0.00694', 'time': 1706192836836},\n",
      "          'BNXUSDT': {'symbol': 'BNXUSDT', 'price': '0.285600', 'time': 1706192859765},\n",
      "          '...': '......'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "tickerPricesMap_result = market.get_tickerPricesMap()\n",
    "eprint(tickerPricesMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f10e88ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'ETHUSDT': {'symbol': 'ETHUSDT', 'price': '2208.68', 'time': 1706192867298},\n",
      "          'BTCUSDT': {'symbol': 'BTCUSDT', 'price': '39990.70', 'time': 1706192866967}},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "tickerPricesMap_result = market.get_tickerPricesMap(symbols=['BTCUSDT', 'ETHUSDT'])\n",
    "eprint(tickerPricesMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de645de6",
   "metadata": {},
   "source": [
    "## 9. 获取深度信息 get_depth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "38978b85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'lastUpdateId': 3886060389175,\n",
      "          'E': 1706192868070,\n",
      "          'T': 1706192868014,\n",
      "          'bids': [['39985.10', '2.376'],\n",
      "                   ['39985.00', '0.003'],\n",
      "                   ['39983.40', '1.600'],\n",
      "                   ['39983.10', '0.240'],\n",
      "                   ['39982.70', '0.783'],\n",
      "                   '......'],\n",
      "          'asks': [['39985.20', '0.951'],\n",
      "                   ['39985.30', '0.009'],\n",
      "                   ['39986.00', '0.050'],\n",
      "                   ['39986.90', '0.005'],\n",
      "                   ['39987.20', '0.003'],\n",
      "                   '......']},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "depth_result = market.get_depth(symbol='BTCUSDT')\n",
    "eprint(depth_result, data_length=5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
